package com.youi.web;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.entity.*;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.service.*;
import com.youi.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 团长提现申请控制层
* @author Administrator
*/
@Controller
public class LeaderwithdrawController  extends HttpRequestManage{

		@Autowired
		private LeaderwithdrawService leaderwithdrawService;
		@Autowired
		private LeaderprofitService leaderprofitService;
		@Autowired
		private TremittanceService tremittanceService;
		@Autowired
		private SetleaderlevelService setleaderlevelService;
		@Autowired
		private SysuserService sysuserService;
		@Autowired
		private LeaderprofitdetailService leaderprofitdetailService;

		/**
		 * I-120
		 * 审核团长提现申请
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/auditLeaderwithdrawByIds")
		public String delLeaderwithdrawByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			String ids=getString("ids",request);
			Integer status=optInteger(Common.STATUS, request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Leaderwithdraw> leaderwithdrawList= leaderwithdrawService.getByParams(params);
			if (leaderwithdrawList==null||leaderwithdrawList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			DecimalFormat dFormat=new DecimalFormat("0.00");
			for (Leaderwithdraw leaderwithdraw : leaderwithdrawList) {
				if (leaderwithdraw.getStatus()!=0) {
					return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "无法重复审核"); 
				}
				if (status==-1) {
					params=new HashMap<String, Object>();
					params.put(Common.DELSTATUS, DelStatus.NODEL);
					params.put("leaderId", leaderwithdraw.getLeaderId());
					Leaderprofit leaderprofit=leaderprofitService.getByParam(params);
					if (leaderprofit!=null) {						
						leaderprofit.setBalance(ObjectUtil.sciCal(leaderprofit.getBalance()+leaderwithdraw.getAmount(), 2));
						leaderprofit.setWithdres(ObjectUtil.sciCal(leaderprofit.getWithdres()-leaderwithdraw.getAmount(), 2));			
						leaderprofitService.saveOrUpdate(leaderprofit);
					}
					leaderprofitdetailService.saveOrUpdate(new Leaderprofitdetail(leaderwithdraw.getLeaderId(), 0, leaderwithdraw.getAmount(), "提现申请被退回", leaderwithdraw.getOrderNo()));
				}else if (status==1) {
					Double profitRatio=setleaderlevelService.getLeaderRate(1)+setleaderlevelService.getLeaderRate(2);
					Tremittance tremittance=new Tremittance(leaderwithdraw.getLeaderId(), 2, leaderwithdraw.getOrderNo(), leaderwithdraw.getAmount(),
					profitRatio, leaderwithdraw.getLeaderName(), 1);
					tremittance.setLeaderAmount(leaderwithdraw.getRealAnt());
					tremittance.setServiceCharge(leaderwithdraw.getCommission());
					tremittanceService.saveOrUpdate(tremittance);
				}
				leaderwithdraw.setAuditorId(sysuser.getId());
				leaderwithdraw.setGmtAudit(new Date());
				leaderwithdraw.setGmtModify(new Date());
				leaderwithdraw.setStatus(status);
			}
			leaderwithdrawService.saveOrUpdateAll(leaderwithdrawList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-118
		 * 查询团长提现申请列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getLeaderwithdraw")
		public String getLeaderwithdraw(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			Integer status=optInteger(Common.STATUS, request);
			String des=optString("des",request,Common.DESC);
			String orderNo=optString("orderNo", request);
			String dateNo=optString("dateNo", request);
			String pms=optString("pms",request);
			String leaderStr=optString("leaderStr",request);
			String authStoreCode=optString("authStoreCode",request);
			String start=optString("start",request);
			String end=optString("end",request);
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			//状态
			if (status!=null) {
				prams.put(Common.STATUS, status);
			}
			//提现单号
			if (orderNo!=null&&orderNo.trim().length()>0) {
				prams.put("orderNo", orderNo);
			}
			//期号
			if (dateNo!=null&&dateNo.trim().length()>0) {
				prams.put("dateNo", dateNo);
			}
			//门店编号
			if (authStoreCode!=null&&authStoreCode.trim().length()>0) {
				prams.put("belongStoreCode", authStoreCode);
			}
			//团长
			if (leaderStr!=null&&leaderStr.trim().length()>0) {
				List<Long> leaderIdList=leaderwithdrawService.getLeaderIdsList(leaderStr);
				if (leaderIdList!=null&&leaderIdList.size()>0) {
					prams.put("leaderId", leaderIdList);
				}else {
					prams.put("leaderId", 0);
				}
			}
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Leaderwithdraw> leaderwithdrawList=leaderwithdrawService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (leaderwithdrawList==null||leaderwithdrawList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=leaderwithdrawService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, leaderwithdrawList);		//jsonObjectList json对象集合
			leaderwithdrawService.getLeaderwithdraw(leaderwithdrawList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}
		
		/**
		 * I-119
		 * 导出团长提现申请列表excel
		*/
		@ResponseBody
		@RequestMapping("toExcelLeaderwithdraw")
		public String toExcelLeaderwithdraw(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{			
			Integer status=optInteger(Common.STATUS, request);
			String des=optString("des",request,Common.DESC);
			String orderNo=optString("orderNo", request);
			String dateNo=optString("dateNo", request);
			String pms=optString("pms",request);
			String leaderStr=optString("leaderStr",request);
			String authStoreCode=optString("authStoreCode",request);
			String start=optString("start",request);
			String end=optString("end",request);
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			//状态
			if (status!=null) {
				prams.put(Common.STATUS, status);
			}
			//提现单号
			if (orderNo!=null&&orderNo.trim().length()>0) {
				prams.put("orderNo", orderNo);
			}
			//期号
			if (dateNo!=null&&dateNo.trim().length()>0) {
				prams.put("dateNo", dateNo);
			}
			//门店编号
			if (authStoreCode!=null&&dateNo.trim().length()>0) {
				prams.put("authStoreCode", authStoreCode);
			}
			//团长
			if (leaderStr!=null&&leaderStr.trim().length()>0) {
				List<Long> leaderIdList=leaderwithdrawService.getLeaderIdsList(leaderStr);
				if (leaderIdList!=null&&leaderIdList.size()>0) {
					prams.put("leaderId", leaderIdList);
				}else {
					prams.put("leaderId", 0);
				}
			}
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Leaderwithdraw> leaderwithdrawList=leaderwithdrawService.getByParamsAll(prams, searchPram, sortPram, startTime, endTime);
			if (leaderwithdrawList==null||leaderwithdrawList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			leaderwithdrawService.getLeaderwithdraw(leaderwithdrawList);
			leaderwithdrawService.toExcelLeaderwithdraw(request, leaderwithdrawList);
			modelMap.put(CommonInfoUtil.JSONCODE, CommonInfoUtil.SUCCESS);
			modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

	/**
	 * I-119
	 * 导出团长提现申请列表excel--new
	 */
	@ResponseBody
	@RequestMapping(value = "sysUserGetInfo/exportLeaderWithdraw", produces = "application/json")
	public void exportLeaderWithdraw(@RequestBody Map<String, Object> param, HttpServletRequest request, HttpServletResponse response) throws BusinessException, ParseException {
		List<Leaderwithdraw> leaderwithdrawList=leaderwithdrawService.getExportLeaderWithdraw(param);
		if (CollectionUtils.isEmpty(leaderwithdrawList)) {
			throw new BusinessException(-1, "没有要导出的数据");
		}

		try {
			String fileName = "佳选官提现" + DateUtil.simpyMdHHmmss.format(new Date());
			ExportParams exportParams = new ExportParams(null, "佳选官提现", ExcelType.XSSF);
			exportParams.setStyle(ExcelStyleUtil.class);

			EasyPoiUtil.exportExcel(fileName, exportParams, Leaderwithdraw.class, leaderwithdrawList, request, response);
		} catch (Exception ex) {
			throw new BusinessException(-1, "导出数据失败");
		}
	}

		/**
		 *根据id查询团长提现申请
		*/
		@ResponseBody
		@RequestMapping("getLeaderwithdrawById")
		public String getLeaderwithdrawById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Leaderwithdraw leaderwithdraw=leaderwithdrawService.get(id);
			if (leaderwithdraw==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderwithdraw);//jsonObject	json对象
		}

		/**
		 * 查询所有团长提现申请
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllLeaderwithdraw")
		public String getAllLeaderwithdraw(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Leaderwithdraw> leaderwithdrawList= leaderwithdrawService.getByParams(null);
			if (leaderwithdrawList==null||leaderwithdrawList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderwithdrawList);//jsonObjectList	json对象集合
		}
}
